//
//  RecommCell.m
//  project_delicious
//
//  Created by 章历杰 on 15/3/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RecommCell.h"
#import "UIImageView+AFNetworking.h"
#import "UIColor+AddColor.h"
@implementation RecommCell
- (void)dealloc
{
    [_array release];
    [_details release];
    //[_introduce release];
    [_backG release];
    [_head release];
    //[_comment release];
    [_favo release];
    [_favoC release];
    //Block_release(_block);
    
    [_location release];
    [_time release];
    [_name release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //数组
        self.array = [NSMutableArray array];
        //details
        self.details = [[Details alloc] init];
        [_details release];
        //背景图
        self.backG = [[UIImageView alloc] init];
        //self.backG.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:self.backG];
        [self.backG release];
        //头像
        self.head = [[UIImageView alloc] init];
        self.head.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:self.head];
        [self.head release];
        //位置
        self.location = [[UILabel alloc] init];
        //self.location.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:self.location];
        [self.location release];
        
        //时间
        self.time = [[UILabel alloc] init];
        //self.time.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:self.time];
        [self.time release];
        //赞
        self.favo = [[UIImageView alloc] init];
        //self.favo.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.favo];
        [self.favo release];
        //赞数
        self.favoC = [[UILabel alloc] init];
        self.favoC.backgroundColor = [UIColor colorFromHexCode:@"#f58f98"];
        [self.contentView addSubview:self.favoC];
        [self.favoC release];
        //评论
        //self.comment = [[UIImageView alloc] init];
        //self.comment.backgroundColor = [UIColor whiteColor];
        //[self.contentView addSubview:self.comment];
        //[self.comment release];
        //菜名
        self.name = [[UILabel alloc] init];
        //self.name.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:self.name];
        [self.name release];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //背景图
    self.backG.frame = CGRectMake(5, 0, self.contentView.frame.size.width-10, self.contentView.frame.size.height-60);
    _backG.layer.cornerRadius = 15;
    //菜名
    self.name.frame = CGRectMake(20, _backG.frame.origin.y+_backG.frame.size.height, self.contentView.frame.size.width-10, 30);
    //头像
    self.head.frame = CGRectMake(20, _backG.frame.size.height-50, 40, 40);
    self.head.layer.cornerRadius = 20;
    //位置
    self.location.frame = CGRectMake(80, _head.frame.origin.y, 200, 15);
    //位置图标
    UIImageView *locationPic = [[UIImageView alloc] initWithFrame:CGRectMake(65, _location.frame.origin.y, 15, 15)];
    locationPic.image = [UIImage imageNamed:@"locationPic.png"];
    //locationPic.backgroundColor = [UIColor blackColor];
    [self.contentView addSubview:locationPic];
    [locationPic release];
    //时间
    self.time.frame = CGRectMake(80, _location.frame.origin.y+20, 150, 15);
    //时间图标
    UIImageView *timePic = [[UIImageView alloc] initWithFrame:CGRectMake(65, _time.frame.origin.y, 15, 15)];
    timePic.image = [UIImage imageNamed:@"timePic.png"];
    [self.contentView addSubview:timePic];
    [timePic release];
    //赞
    self.favo.frame = CGRectMake(5+_backG.frame.size.width-45, _backG.frame.size.height-40, 30, 30);
    self.favo.layer.cornerRadius = 15;
    self.favo.backgroundColor = [UIColor whiteColor];
    //赞数
    self.favoC.frame = CGRectMake(5+_backG.frame.size.width-30, _backG.frame.size.height-50, 18, 18);
    self.favoC.layer.cornerRadius = 9;
    self.favoC.layer.masksToBounds = YES;
    self.favoC.font = [UIFont systemFontOfSize:12];
    self.favoC.textAlignment = NSTextAlignmentCenter;
    self.favoC.textColor = [UIColor whiteColor];
    //评论
    //self.comment.frame = CGRectMake(5+_backG.frame.size.width-95, _backG.frame.size.height-40, 30, 30);
    //self.comment.layer.cornerRadius = 15;
    
    //self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMG_1345.JPG"]];
}

- (void)setDetails:(Details *)details
{
 
    if (_details != details) {
        [_details release];
        _details = [details retain];
    }
    //背景图
    NSString *str = self.details.main_photo_url;
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    [_backG setImageWithURL:url];
    _backG.layer.masksToBounds = YES;
    //菜名
    _name.text = self.details.dish_name;
    _name.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    //头像
    NSString *strHead = self.details.ico;
    strHead = [strHead stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *urlHead = [NSURL URLWithString:strHead];
    [_head setImageWithURL:urlHead];
    _head.layer.masksToBounds = YES;
    //赞
    _favo.image = [UIImage imageNamed:@"favorite.png"];
    _favo.layer.masksToBounds = YES;
    _favo.userInteractionEnabled = YES;
    UITapGestureRecognizer *favoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(favoTapAction)];
    [_favo addGestureRecognizer:favoTap];
    [favoTap release];
    //赞数
    NSString *string = self.details.award_count;
    NSInteger a = [string integerValue];
    if (a > 99) {
        _favoC.text = @"...";
    } else {
        _favoC.text = string;
    }
    //评论
    //_comment.image = [UIImage imageNamed:@"comment.png"];
    //_comment.layer.masksToBounds = YES;
    //位置
    _location.text = self.details.shop_name;
//    _location.textAlignment = NSTextAlignmentRight;
    _location.font = [UIFont systemFontOfSize:14];
    [_location setTextColor:[UIColor colorFromHexCode:@"#fffffb"]];
    
    //时间
    _time.text = self.details.time_tips;
//    _time.textAlignment = NSTextAlignmentRight;
    _time.font = [UIFont systemFontOfSize:14];
    [_time setTextColor:[UIColor colorFromHexCode:@"#fffffb"]];
}

- (void)favoTapAction
{
    NSString *string = self.details.award_count;
    NSInteger a = [string integerValue];
    if (a > 99) {
        _favoC.text = @"...";
    } else {
        if (!_flag) {
            _favoC.text = [NSString stringWithFormat:@"%d", a+1];
            _favo.image = [UIImage imageNamed:@"favorite2.png"];
        } else if (_flag) {
            _favoC.text = [NSString stringWithFormat:@"%d", a];
            _favo.image = [UIImage imageNamed:@"favorite.png"];
        }
    }
    //_block(_favoC.text);
    
    _flag = !_flag;
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
